﻿

using HalconDotNet;
using System.Diagnostics;

namespace LeaderCCSLaserUI.Models
{
    public class HalconEngine
    {
        WorkerEngine hDevEngine1, hDevEngine2;
        public HalconEngine()
        {
            hDevEngine1 = new WorkerEngine();
            hDevEngine2 = new WorkerEngine();
        }
        public void Load(string procedurePath, string[] procedureNames1, string[] procedureNames2)
        {
            try
            {
                //换路径后，外部函数需要重新加载。
                hDevEngine1.RemoveProcedures();
                for (int i = 0; i < procedureNames1.Length; i++)
                {
                    hDevEngine1.AddProcedure(procedureNames1[i], procedurePath);
                }

                hDevEngine2.RemoveProcedures();
                for (int i = 0; i < procedureNames2.Length; i++)
                {
                    hDevEngine2.AddProcedure(procedureNames2[i], procedurePath);
                }
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
        public void Calc1(string procedureName, HObject img, HTuple dir, out HTuple row,
     out HTuple column,out HTuple angle, out HTuple result)
        {
            hDevEngine1.SetParam(procedureName, "image", img);
            hDevEngine1.SetParam(procedureName, "dir", dir);
            bool r = hDevEngine1.InspectProcedure(procedureName);
            if (r)
            {
                result = hDevEngine1.GetParam<HTuple>(procedureName, "result");
                row = hDevEngine1.GetParam<HTuple>(procedureName, "row");
                column = hDevEngine1.GetParam<HTuple>(procedureName, "column");
                angle = hDevEngine1.GetParam<HTuple>(procedureName, "angle");
            }
            else
            {
                result = new HTuple(0);
                row = new HTuple(0);
                column = new HTuple(0);
                angle = new HTuple(0);
            }
        }
        public void Calc2(string procedureName, HObject img, HTuple dir, out HTuple row,
            out HTuple column, out HTuple angle, out HTuple result)
        {
            hDevEngine2.SetParam(procedureName, "image", img);
            hDevEngine2.SetParam(procedureName, "dir", dir);
            bool r = hDevEngine2.InspectProcedure(procedureName);
            if (r)
            {
                result = hDevEngine2.GetParam<HTuple>(procedureName, "result");
                row = hDevEngine2.GetParam<HTuple>(procedureName, "row");
                column = hDevEngine2.GetParam<HTuple>(procedureName, "column");
                angle = hDevEngine2.GetParam<HTuple>(procedureName, "angle");
            }
            else
            {
                result = new HTuple(0);
                row = new HTuple(0);
                column = new HTuple(0);
                angle = new HTuple(0);
            }
        }
    }
}
